﻿@page "/SysConfig/Edit/{id}"
@using SelfService.ViewModel.SysConfigVMs;
@using SelfService.Model
@using System.Globalization
@inherits BasePage

<ValidateForm @ref="VForm" Model="@_model" OnValidSubmit="@Submit">
    <Row ItemsPerRow="ItemsPerRow.Two" RowType="RowType.Normal">
        @if (_model.Entity.ValueType == SysConfigValueTypeEnum.字符串)
        {
            <BootstrapInput @bind-Value="@_model.Entity.Value"/>
        }

        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.整数)
        {
            <BootstrapInputNumber ShowButton="true" TValue="int" ValueChanged="@IntValueChanged" DisplayText="值"/>
        }

        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.小数)
        {
            <BootstrapInputNumber ShowButton="true" TValue="decimal" ValueChanged="@DecimalValueChanged"  DisplayText="值"/>
        }

        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.分钟 or SysConfigValueTypeEnum.秒)
        {
            <BootstrapInputNumber ShowButton="true" TValue="int" ValueChanged="@IntValueChanged" Min="1" Max="60" DisplayText="值"/>
        }

        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.年)
        {
            <BootstrapInputNumber ShowButton="true" TValue="int" ValueChanged="@IntValueChanged" Min="0" Max="9999" DisplayText="值"/>
        }

        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.月)
        {
            <BootstrapInputNumber ShowButton="true" TValue="int" ValueChanged="@IntValueChanged" Min="1" Max="12" DisplayText="值"/>
        }

        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.日)
        {
            <BootstrapInputNumber ShowButton="true" TValue="int" ValueChanged="@IntValueChanged" Min="1" Max="31" DisplayText="值"/>
        }

        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.小时)
        {
            <BootstrapInputNumber ShowButton="true" TValue="int" ValueChanged="@IntValueChanged" Min="0" Max="24" DisplayText="值"/>
        }

        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.日期)
        {
            <DateTimePicker TValue="DateTime" ValueChanged="@DateTimeValueChanged" ViewMode="DatePickerViewMode.DateTime"  DisplayText="值"/>
        }

        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.布尔型)
        {
            <Switch ValueChanged="@BoolValueChanged" OnColor="Color.Success" OnText="是" OffText="否"  DisplayText="值"></Switch>
        }
        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.文件)
        {
            <WTUploadImage TValue="Guid?" Value="FileId" ValueChanged="FileValueChanged" thumb-width="128"/>
        }
        <BootstrapInput @bind-Value="@_model.Entity.Key"  />
        <Select @bind-Value="@_model.Entity.ValueType"  PlaceHolder="@WtmBlazor.Localizer["Sys.PleaseSelect"]"></Select>
        <BootstrapInput @bind-Value="@_model.Entity.Description"  />
        <Select @bind-Value="@_model.Entity.GroupId" Items="@_allSysConfigGroups" PlaceHolder="@WtmBlazor.Localizer["Sys.PleaseSelect"]"></Select>
    </Row>
    <div class="modal-footer table-modal-footer">
        <Button Color="Color.Warning" Icon="fa fa-close" Text="@WtmBlazor.Localizer["Sys.Close"]" OnClick="OnClose"></Button>
        <Button Color="Color.Primary" ButtonType="ButtonType.Submit" Icon="fa fa-save" Text="@WtmBlazor.Localizer["Sys.Edit"]" IsAsync="true"></Button>
    </div>
</ValidateForm>

@code {

    private SysConfigVm _model = null;
    private ValidateForm VForm { get; set; }
    [Parameter]
    public string Id { get; set; }
    private Guid? FileId { get; set; }

    private List<SelectedItem> _allSysConfigGroups = new List<SelectedItem>();

    protected override async Task OnInitializedAsync()
    {
        _allSysConfigGroups = await WtmBlazor.Api.CallItemsApi("/api/SysConfig/GetSysConfigGroups", placeholder: WtmBlazor.Localizer["Sys.PleaseSelect"]);
        var rv = await WtmBlazor.Api.CallAPI<SysConfigVm>($"/api/SysConfig/{Id}");
        _model = rv.Data;
        @if (_model.Entity.ValueType is SysConfigValueTypeEnum.文件)
        {
            FileId = Guid.Parse(_model.Entity.Value);
        }
        await base.OnInitializedAsync();
    }

    private Task FileValueChanged(Guid? id)
    {
        _model.Entity.Value = id.ToString();
        return Task.CompletedTask;
    }


    private Task IntValueChanged(int number)
    {
        _model.Entity.Value = number.ToString();
        return Task.CompletedTask;
    }

    private Task DecimalValueChanged(decimal number)
    {
        _model.Entity.Value = number.ToString(CultureInfo.InvariantCulture);
        return Task.CompletedTask;
    }


    private Task DateTimeValueChanged(DateTime ts)
    {
        _model.Entity.Value = ts.ToString("yyyy-MM-dd HH:mm:ss");
        return Task.CompletedTask;
    }

    private Task BoolValueChanged(bool value)
    {
        _model.Entity.Value = value ? "true" : "false";
        return Task.CompletedTask;
    }



    private async Task Submit(EditContext context)
    {
        await PostsForm(VForm, $"/api/SysConfig/edit", (s) => "Sys.OprationSuccess", method: HttpMethodEnum.PUT);
    }

    public void OnClose()
    {
        CloseDialog();
    }

}
